package com.danco.planner.essences.arraylists;

import java.util.ArrayList;
import java.util.Date;

import com.danco.planner.essences.Course;
import com.danco.planner.sortings.sortby.SortByDate;
import com.danco.planner.sortings.sortby.SortByName;
import com.danco.planner.sortings.sortby.SortBySection;
import com.danco.planner.sortings.sortby.SortBySpeaker;
import com.danco.planner.sortings.sortby.SortByStudentsCount;


public class CoursesList extends ArrayList<Course> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	//In truth, I can not imagine why i need this ID

	public CoursesList getCoursesWillGoAfterTheDate(Date date) {
		
		CoursesList result = new CoursesList();
		for (Course course : this) {
			if (date.getTime() <= course.getStartDate().getTime()) {
				result.add(course);
			}
		}
		return result;
	}
	public CoursesList getCoursesWasFinishedInDate(Date date) {
		
		CoursesList result = new CoursesList();
		for (Course course : this) {
			if (date.getTime() >= course.getFinishDate().getTime()) {
				result.add(course);
			}
		}
		return result;
	}
	public CoursesList getCoursesIsNowAreGout(Date currentDate) {
		
		CoursesList inverse = getCoursesWasFinishedInDate(currentDate);
		CoursesList result = ((CoursesList)clone()); 
		result.removeAll(inverse);
		
		inverse = result.getCoursesWillGoAfterTheDate(currentDate);
		result.removeAll(inverse);
		
		return result;
	}
	public CoursesList getCoursesIsNowAreGoint() {
		return getCoursesIsNowAreGout(new Date());
	}
	public CoursesList getCoursesBetweenDates(Date fDate, Date sDate) {
		
		if (fDate.getTime() > sDate.getTime()) {
			Date temp = fDate;
			fDate = sDate;
			sDate = temp;
		}
		
		CoursesList result = getCoursesWillGoAfterTheDate(fDate);
		return result.getCoursesWasFinishedInDate(sDate);
	}

	public void sortByStartDate() {
		this.sort(new SortByDate());
	}
	public void sortByStudentsCount() {
		this.sort(new SortByStudentsCount());
	}
	public void sortBySection() {
		this.sort(new SortBySection());
	}
	public void sortBySpeaker() {
		this.sort(new SortBySpeaker());
	}
	public void sortByName() {
		this.sort(new SortByName());
	}
	
	@Override
	public String toString() {
		
		StringBuilder stb = new StringBuilder();
		for (Course course : this) {
			stb.append(course.toString() + "\n\t");
		}
		stb.deleteCharAt(stb.length()-1);
		return stb.toString();
	}
}
